package com.qrcode.zbar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qrcode.zbar.osbar.OsBarUtil;

import client.PhotoClient;
import client.PhotoSelectResultBean;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;


public class ZbarCodeActivity extends AppCompatActivity implements QRCodeView.Delegate, View.OnClickListener {
    private static final String TAG = ZbarCodeActivity.class.getSimpleName();
    private ZBarView mZBarView;
    private View viewPowerZbar, viewBackZbar;
    private LinearLayout lineRightZbar,lineLeftZbar;
    private TextView tvBackZbar, tvTitleZbar,tvRightZbar;
    private RelativeLayout barTopZbar;
    private boolean isFlashLight = false;
    private Activity activity;

    public static ZbarLiveEvent<ZbarResultBean> qrcodeZbarResult = new ZbarLiveEvent<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zbar_qrcode);

        barTopZbar = findViewById(R.id.rey_zbar_top);
        mZBarView = findViewById(R.id.zbarView);
        viewPowerZbar = findViewById(R.id.view_zbar_power);
        viewBackZbar = findViewById(R.id.view_zbar_back);
        tvBackZbar = findViewById(R.id.tv_zbar_back);
        tvTitleZbar = findViewById(R.id.tv_zbar_title);
        tvRightZbar=findViewById(R.id.tv_zbar_right);
        lineRightZbar = findViewById(R.id.line_zbar_right);
        lineLeftZbar = findViewById(R.id.line_zbar_left);

        activity=this;

        init();
    }

    private void init() {
        try {
            lineLeftZbar.setOnClickListener(this);
            barTopZbar.setBackgroundColor(ZbarClient.getClient().getBarBgColor());
            if(ZbarClient.getClient().isToOsBar()){
                OsBarUtil.setStatusBarColor(this, ZbarClient.getClient().getBarTextColor(),ZbarClient.getClient().getBarBgColor());
            }
            viewBackZbar.setBackgroundResource(ZbarClient.getClient().getImgBack());
            //返回键
            if (ZbarClient.getClient().isShowBackText()) {
                tvBackZbar.setText(ZbarClient.getClient().getBarBackText());
                tvBackZbar.setVisibility(View.VISIBLE);
                tvBackZbar.setTextColor(ZbarClient.getClient().getBarTextColor());
            } else {
                tvBackZbar.setVisibility(View.INVISIBLE);
            }
            //标题
            tvTitleZbar.setText(ZbarClient.getClient().getBarTitleText());
            tvTitleZbar.setTextColor(ZbarClient.getClient().getBarTextColor());
            //右部相册
            if(ZbarClient.getClient().isOpenPhoto()){
                lineRightZbar.setVisibility(View.VISIBLE);
                tvRightZbar.setTextColor(ZbarClient.getClient().getBarTextColor());
                lineRightZbar.setOnClickListener(this);
            }else{
                lineRightZbar.setVisibility(View.INVISIBLE);
            }
            //设置扫描
            mZBarView.getScanBoxView().setQRCodeTipText(ZbarClient.getClient().getScanTipText());

            mZBarView.getScanBoxView().setCornerColor(ZbarClient.getClient().getScanCornerColor());
            mZBarView.getScanBoxView().setBorderColor(ZbarClient.getClient().getScanBorderColor());
            mZBarView.getScanBoxView().setScanLineColor(ZbarClient.getClient().getScanLineColor());
            mZBarView.getScanBoxView().setTipTextColor(ZbarClient.getClient().getScanTipTextColor());
            mZBarView.getScanBoxView().setIsBarcode(ZbarClient.getClient().isBarcode());
            mZBarView.getScanBoxView().setAutoZoom(ZbarClient.getClient().isAutoZoom());

            //其他设置
            viewPowerZbar.setOnClickListener(this);
            mZBarView.setDelegate(this);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "init Exception:" + e.getMessage());
        }
        Log.e(TAG, "init finish 1" );
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart 调用识别" );
        mZBarView.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
//        mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别
        mZBarView.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }

    @Override
    protected void onStop() {
        mZBarView.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mZBarView.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.e(TAG, "result:" + result);
        if (qrcodeZbarResult.hasObservers()) {
            qrcodeZbarResult.postValue(new ZbarResultBean(ZbarClient.getClient().getZbarTag(),result));
        }
        finish();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
        String tipText = mZBarView.getScanBoxView().getTipText();
        String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
        Log.e("zabar环境检测", String.valueOf(isDark));
        if (isDark) {
            if (!tipText.contains(ambientBrightnessTip)) {
                mZBarView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
                viewPowerZbar.setVisibility(View.VISIBLE);
            }
        } else {
            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                mZBarView.getScanBoxView().setTipText(tipText);
                //如果闪光灯未开的情况亮度恢复  则取消掉手电筒显示
            }

            if (!isFlashLight) {
                viewPowerZbar.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Log.e(TAG, "打开相机出错");
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.view_zbar_power) {
            if (!isFlashLight) {
                mZBarView.openFlashlight();
                isFlashLight = true;
            } else {
                mZBarView.closeFlashlight();
                isFlashLight = false;
            }
        } else if (id == R.id.line_zbar_right) {
            getPhotoQrcode();
        }else if (id == R.id.line_zbar_left) {
            finish();
        }
    }


    private void getPhotoQrcode(){
        PhotoClient.getClient().selectPhotoResult(activity, new PhotoClient.PhotoResult() {
            @Override
            public void back(PhotoSelectResultBean photoSelectResultBean) {
                Log.e("收到的图像选择回调", "Tag：" + photoSelectResultBean.getTag() + "     Result:" + photoSelectResultBean.getResult());
                if (!photoSelectResultBean.getTag().equals(activity.getClass().getName() )) {
                    return;
                }
                mZBarView.decodeQRCode(photoSelectResultBean.getResult().get(0));
            }
        });

        PhotoClient.getClient().setDefault()
                .setPhotoResultTag(activity.getClass().getName())
                .setMultiSelectPhoto(false)
                .setCropPhoto(false)
                .setTakePhoto(false)
                .setSelectPhotoList(null)
                .setToOsBar(true)
                .startPhotoPicker(activity);
    }




}